<?php 
/**
 * Copyright (c) 2010, Slawomir Mazur <mazursllawek@gmail.com>
 * 
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions 
 * are met:
 * 
 * o Redistributions of source code must retain the above copyright notice, 
 *   this list of conditions and the following disclaimer.
 * o Redistributions in binary form must reproduce the above copyright notice,
 *   this list of conditions and the following disclaimer in the documentation
 *   and/or other materials provided with the distribution.
 * o Neither the name of the Softisland nor the names of its contributors
 *   may be used to endorse or promote products derived from this software
 *   without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 
 * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
/**
 * Enter description here...
 *
 * @package     Cms
 * @copyright   Slawomir Mazur (c) 2010 (http://www.zend.com/zce.php?c=ZEND011643)
 * @license     http://www.opensource.org/licenses/bsd-license.php
 * @author      Slawomir Mazur <mazursllawek@gmail.com>
 */
?>

<?php $user = $this->user()->getUser() ?>

<div class="container">
	<h2>RNAiAtlas - RNAi libraries and Known&amp;Predicted Off-target siRNA Interactions</h2>	
	
	<ul class="network-note">
		<li class="library clear">
			<span>Library: </span>
			<h3>
				<?php echo $this->company()->getCompanyNameByLibraryId($this->library) ?> 
				/ <?php echo $this->library()->getLibraryCreatedDateById($this->library) ?>
			</h3>
		</li>
		<li class="gene clear">
			<span>Gene Name: </span>
			<h3><?php echo $this->gene ?></h3>
		</li>
	</ul>
	
	<div id="note" class="tooltip-note">#<br />#</div>
		
	<div id="cytoscapeweb"><?php echo $this->translate('Network is loading. Please wait.') ?></div>
	<div>
		<a class="back-link" href="<?php echo $this->backURL ?>" title="Go back">Back</a>
	</div>
</div>
<script type="text/javascript">
//<![CDATA[
    var nX = 0;
    var nY = 0;
           
	// id of Cytoscape Web container div
	$(document).ready(function() {
		var div_id = "cytoscapeweb";

		var moveable = false;
		var clicked = false;
		
		$('#' + div_id).mousemove(function(event) {
			nX = event.pageX;
			nY = event.pageY;

			// open tool tip for link click
			if (clicked) {
				return;
			}
			
			if (moveable) {
				$('#note').css({ top: nY + 10, left: nX + 10, display: 'block' });
			} else {
				$('#note').css({ display: 'none' });
			}
		});

		$('#' + div_id).mouseout(function(event) {
			moveable = false;
		});


		// graph options
		var directedLayout = {
			name: 'ForceDirected',
			options: {
				drag:          0.4,
				gravitation:   -5000,
				minDistance:   1,
				maxDistance:   10000,
				mass:          3,
				tension:       0.1,
				iterations:    400,
				maxTime:       30000,
				autoStabilize: false
			}
		}

		var compaundLayout = {
			name: 'CompoundSpringEmbedder',
			options: {
				layoutQuality: 'proof',
				gravitation: -200,
				smartDistance: true,
				centralGravitation: 100,
				centralGravityDistance: 200
			}
		}

		var visualStyle = {
			global: {
				tooltipDelay: 200,
				tooltipFontSize: 20
	        },
	        nodes: {
	        	tooltipText: "geneId: ${gid}<br />geneName: ${name}<br />siRNA: ${sirna}",	        	
		    },
		    edges: {
	        	tooltipText: 
		        	"- Source -<br />" +
                    "geneId: ${source}<br />" +
                    "- Target -<br />" +
                    "geneId: ${target}<br />" +
					"off-target: ${sirna}"
		    }
		}
		
		// initialization options
		var options = {
			nodeTooltipsEnabled: false,
			edgeTooltipsEnabled: false,
			//layout: 'Preset',
			//layout: directedLayout,
			layout: compaundLayout,
			// set the style at initialisation
            //visualStyle: visualStyle,
			//where you have the Cytoscape Web SWF
			swfPath: '<?php echo $this->baseUrl('js/cytoscape/swf/CytoscapeWeb') ?>',
			//where you have the Flash installer SWF
			flashInstallerPath: '<?php echo $this->baseUrl('js/cytoscape/swf/playerProductInstall') ?>'
		};
		
		// init and draw
		var vis = new org.cytoscapeweb.Visualization(div_id, options);		
		
		vis.ready(function() {			
			// add a listener for when nodes and edges are clicked
            vis.addListener("mouseover", "nodes", function(event) {
                handle_in(event);
            })
            .addListener("mouseout", "nodes", function(event) {
				handle_out();
			})
			.addListener("mouseover", "edges", function(event) {
				handle_ein(event);
			})
			.addListener("mouseout", "edges", function(event) {
				handle_out();
			})
			.addListener("click", "nodes", function(event) {
				if (!event.target.data['isParent']) {
					clicked = false;
					$('#note').css({ top: nY + 10, left: nX + 10, display: 'block' });
					handle_in(event);
					clicked = true;
				} else {
					clicked = false;					
				}
				$('#' + div_id).trigger('mousemove', event);
			})
			.
			addListener("click", function(event) {
				clicked = false;
				$('#' + div_id).trigger('mousemove');
			});

			function handle_in(event) {
                var target = event.target;  

				if (target.data['isParent'] && !clicked) {
					$('#note').html('');
					moveable = false;
					return;
				}

				if (clicked) {
					return;
				}
                
				moveable = true;
                
                $('#note').html(
                    'Gene ID: ' + getGeneIdLink(target.data['gid']) + '<br />' +
                    'Gene symbol: ' + getGeneSymbolLink(target.data['name']) + '<br />' +
					'siRNA: ' + target.data['sirna']
				);
			}

			function handle_ein(event) {
				var target = event.target;

				if (clicked) {
					return;
				}
				
				moveable = true;
				
                <?php if ($user) : ?>
	                $('#note').html(
	                    '#Source' + '<br />' +
	                    'Gene Id: ' + target.data['source'].replace('node_', '').replace('_', '<br />oligo: ') + '<br />' +
	
	                    '#Target' + '<br />' +
	                    'Gene Id: ' + target.data['target'].replace('node_', '').replace('_', '<br />oligo: ') + '<br />'
					);
				<?php else : ?>
					moveable = false;				
				<?php endif ?>
			}
			
            function handle_out() { 
				moveable = false;
            }
		});

		<?php $file = $this->networkSource ?>
		<?php if (!empty($file)) : ?>
		$.ajax({
			url : '<?php echo $this->baseUrl($file) ?>',
			dataType: 'text',
			success: function(data) {
				options.network = data;
				vis.draw(options);
			}
		});
		<?php else : ?>
		$('#cytoscapeweb').css({ height: '100px' });
		$('#cytoscapeweb').html('<h3>Empty data!</h3>');
		<?php endif ?>
	});

	function getGeneIdLink(id) {
		if (id) {
			return '<a class="popup-link" href="http://www.ncbi.nlm.nih.gov/gene/' + id + '">' + id + '</a>';
		}
		return '-';
	}

	function getGeneSymbolLink(symbol) {
		return '<a class="popup-link" href="' + '<?php echo $this->baseUrl('index/result/q/') ?>' + symbol + '/a/6">' + symbol + '</a>';
	}
//]]>
</script>